package switchviz.core.view;

import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;

import org.jdom.JDOMException;

import switchviz.core.scenario.ScenarioDataModel;
import switchviz.gui.component.JLabeldComponent;
import switchviz.gui.component.JXMLComboBox;
import switchviz.gui.component.PaddingPanel;
import switchviz.gui.component.SlidePanel;

public class ViewScenarioConfiguration extends PaddingPanel implements ItemListener
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -363789334591129512L;
	private JXMLComboBox _cmbview = null;
	private SlidePanel _slpanel = null;
	private ScenarioDataModel _scenariomodel = null;
	private JLabeldComponent _cbvisualize = null;
	
	public ViewScenarioConfiguration(ScenarioDataModel scenedm) throws JDOMException, IOException
	{
		_scenariomodel = scenedm;
		initialize();
	}
	
	public void reload() throws JDOMException, IOException
	{
		_cmbview.reload();
	}
	
	public void addItemListener(ItemListener l)
	{
		_cmbview.getComboBox().addItemListener(l);
	}
	
	public String[] getAliasList()
	{
		if(getViewDataModel() == null) return null;
		
		View view = new View(getViewDataModel());
		
		if(view.getSignalList() == null) return null;
		
		String[] aliaslist = new String[view.getSignalList().size()];
		for(int i = 0; i < aliaslist.length; ++i)
			aliaslist[i] = view.getSignalList().get(i).getAliasName();
		
		return aliaslist;
	}
	
	public void changeVisualizeRecordState(boolean state)
	{
		if(state)
		{
			_cbvisualize.getCheckBox().setSelected(true);
			_cbvisualize.getCheckBox().setEnabled(false);
		}
		else
		{
			_cbvisualize.getCheckBox().setSelected(_scenariomodel.isVisualize());
			_cbvisualize.getCheckBox().setEnabled(true);
		}
	}
	
	private void initialize() throws JDOMException, IOException
	{
		Dimension labelwidth = new Dimension(250, 20);
		_cmbview = new JXMLComboBox("Select View: ", JXMLComboBox.VIEWLIST, labelwidth);
		_cbvisualize = new JLabeldComponent("Visualize: ", JLabeldComponent.CHECKBOX, labelwidth);
		_slpanel = new SlidePanel("Render Rate (FPS): ", 0, 60, 30, labelwidth);
		_cbvisualize.getCheckBox().addItemListener(this);
		
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		this.setBorder(BorderFactory.createTitledBorder("Select View"));
		this.setPreferredSize(new Dimension(800, 160));
		this.setMaximumSize(new Dimension(800, 160));
		this.add(_cbvisualize);
		this.add(_cmbview);
		this.add(_slpanel);
		
		_cbvisualize.getCheckBox().setSelected(_scenariomodel.isVisualize());
	}
	
	public void checkInitViz()
	{
		if(_cmbview.getComboBox().getItemCount() > 0)
		{
			_cmbview.getComboBox().setSelectedIndex(-1);
			_cmbview.getComboBox().setSelectedIndex(0);
		}
	}
	
	public ScenarioDataModel getScenarioModel()
	{
		_scenariomodel.setRenderrate(_slpanel.getValue());
		return _scenariomodel;
	}
	
	public ViewDataModel getViewDataModel()
	{
		return (ViewDataModel)_cmbview.getComboBox().getSelectedItem();
	}
	
	public boolean checkVisualize()
	{
		boolean retval = _cbvisualize.getCheckBox().isSelected();
		if(retval)
		{
			_cmbview.getComboBox().setEnabled(true);
			_slpanel.getSlider().setEnabled(true);
			_scenariomodel.setVisualize(true);
		}
		else
		{
			_cmbview.getComboBox().setEnabled(false);
			_slpanel.getSlider().setEnabled(false);
			_scenariomodel.setVisualize(false);
		}
		return retval;
	}
	
	public void itemStateChanged(ItemEvent evt) 
	{
		checkVisualize();
	}
}
